package 并发编程06.线程池技术;

import java.util.*;

/**
 * 线程池
 * @author ：idea
 * @date：2018/8/12
 */
public class DeafultThreadPool<Job extends Runnable> implements ThreadPool<Job> {

    private final static int DEAFULT_MIN_THREAD_SIZE=5;
    private final static int DEAFULT_MAX_THREAD_SIZE=25;

    private int workerNums=DEAFULT_MIN_THREAD_SIZE;

    //工作任务集合
    private final LinkedList<Job> jobLinkedList=new LinkedList<>();

    //工作者的集合
    private final List<Worker> workerLinkedList= Collections.synchronizedList(new ArrayList<>());

    public DeafultThreadPool(int size){
        size=size>DEAFULT_MAX_THREAD_SIZE?DEAFULT_MAX_THREAD_SIZE:
                (size<DEAFULT_MIN_THREAD_SIZE?DEAFULT_MIN_THREAD_SIZE:size);
        initiallzeWorkers(size);
        workerNums=size; //初始化worker数
    }


    @Override
    public void execute(Job job) {
        if(job!=null) {
            synchronized (jobLinkedList) {
                jobLinkedList.addLast(job);
                jobLinkedList.notify();
            }
        }
    }

    @Override
    public void shutdown() {
        for (Worker worker : workerLinkedList) {
            worker.shutdown();
        }
    }

    @Override
    public void addWorkers(int nums) {
        if(nums<0||nums+this.workerLinkedList.size()>DEAFULT_MAX_THREAD_SIZE){
            //如果增加数目不合理，则默认增加一个工作者
            nums=1;
        }else{
            initiallzeWorkers(nums);
            workerNums+=nums;
        }
    }

    @Override
    public void removeWorker(int nums) {
        if(nums<0||this.workerLinkedList.size()-nums>DEAFULT_MIN_THREAD_SIZE){
            //如果增加数目不合理，则默认增加一个工作者
            nums=1;
        }
        for(int i=0;i<nums;i++){
            Worker worker=workerLinkedList.get(i);
            workerLinkedList.remove(i);
            worker.shutdown();
            workerNums--;
        }
    }

    @Override
    public int getJobSize() {
        return jobLinkedList.size();
    }


    //初始化线程工作者的内容
    public void initiallzeWorkers(int nums){
        for(int i=0;i<nums;i++){
            Worker worker=new Worker();
            workerLinkedList.add(worker);
            Thread thread=new Thread(worker,"threadpool-worker-"+i);
            thread.start();
        }
    }

    //工作者，负责消费类型的任务
    class Worker implements Runnable{

        private volatile boolean running=true;
        @Override
        public void run() {
            while(running){
                Job job=null;
                synchronized (jobLinkedList) {
                    //如果任务为空，则进入等待静止状态
                    while (jobLinkedList.isEmpty()){
                        try {
                            jobLinkedList.wait();
                        } catch (InterruptedException e) {
                            //感知到外界的中断操作，返回
                            Thread.currentThread().interrupt();
                            return;
                        }
                    }
                    job=jobLinkedList.removeFirst();
                }
                //这里面才开始执行job任务
                if(job!=null){
                    job.run();
                }
            }
        }

        //关闭任务执行
        public void shutdown(){
            running=true;
        }
    }

}
